package com.heima.admin.controller.v1;


import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  // @ResponseBody + @Controller
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理controller",tags = "频道管理controller",description = "频道管理controller")
public class ChannelController {

    @Autowired
    private ChannelService channelService;


    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(channelService.getById(id));
    }


    @ApiOperation("条件分页查询频道列表")
    @PostMapping("/list")
    /**
     *  @RequestParam：如果前端传的参数是 From表单格式或者参数也在路径上但是加了一个问号比如：？name=xxx&age=xxx  这个时候用这个注解
     * @PathVariable：如果前端传的参数是在路径上比如：/list/{id}  这个时候就用这个注解
     * @RequestBody:前端如果传的是json对象  参数就用这个注解来接
     */
    public ResponseResult list(@RequestBody ChannelDTO dto){
        return channelService.findListByQuery(dto);
    }


    @ApiOperation(value = "保存频道信息",notes = "保存时 会校验：频道名称不能为空，频道名称不能超过10个字符，名称不能重复")
    @PostMapping("/save")
    public ResponseResult insert(@RequestBody AdChannel channel){
        return channelService.insert(channel);
    }

    @ApiOperation(value = "修改频道信息",notes = "修改时 会校验：如果频道名称有改动，会检查频道名称不为空，频道名称不能超过10个字符，名称不能重复")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel channel){
        return channelService.update(channel);
    }

    @ApiOperation(value = "根据id删除频道",notes = "如果频道有效，不能删除")
    @GetMapping("/del/{id}")
    public ResponseResult delete(@PathVariable Integer id){
        return channelService.delete(id);
    }

    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }


}
